package com.ruoyi.documentStorage.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.courseRecords.domain.UoocCourse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.documentStorage.domain.UoocCourseDocument;
import com.ruoyi.documentStorage.service.IUoocCourseDocumentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 课程文件Controller
 * 
 * @author ruoyi
 * @date 2025-08-04
 */
@RestController
@RequestMapping("/course/document")
public class UoocCourseDocumentController extends BaseController
{
    @Autowired
    private IUoocCourseDocumentService uoocCourseDocumentService;

    /**
     * 查询课程文件列表
     */
    @PreAuthorize("@ss.hasPermi('course:document:list')")
    @GetMapping("/list")
    public TableDataInfo list(UoocCourseDocument uoocCourseDocument)
    {
        startPage();
        List<UoocCourseDocument> list = uoocCourseDocumentService.selectUoocCourseDocumentList(uoocCourseDocument);
        return getDataTable(list);
    }
    /**
     * 根据课程ID查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('course:document:list')")
    @GetMapping("/course/{courseId}")
    public TableDataInfo getByCourseId(@PathVariable("courseId") Long courseId) {
        UoocCourseDocument query = new UoocCourseDocument();
        query.setCourseId(courseId);
        startPage();
        List<UoocCourseDocument> list = uoocCourseDocumentService.selectUoocCourseDocumentList(query);
        return getDataTable(list);
    }

    /**
     * 导出课程文件列表
     */
    @PreAuthorize("@ss.hasPermi('course:document:export')")
    @Log(title = "课程文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UoocCourseDocument uoocCourseDocument)
    {
        List<UoocCourseDocument> list = uoocCourseDocumentService.selectUoocCourseDocumentList(uoocCourseDocument);
        ExcelUtil<UoocCourseDocument> util = new ExcelUtil<UoocCourseDocument>(UoocCourseDocument.class);
        util.exportExcel(response, list, "课程文件数据");
    }

    /**
     * 获取课程文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:document:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(uoocCourseDocumentService.selectUoocCourseDocumentByRecordId(recordId));
    }

    /**
     * 新增课程文件
     */
    @PreAuthorize("@ss.hasPermi('course:document:add')")
    @Log(title = "课程文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UoocCourseDocument uoocCourseDocument)
    {
//        logger.info("accessUrl: {}", accessUrl);
        return toAjax(uoocCourseDocumentService.insertUoocCourseDocument(uoocCourseDocument));
    }

    // 新增文件上传接口
//    @PreAuthorize("@ss.hasPermi('course:document:add')")
//    @Log(title = "课程文件", businessType = BusinessType.INSERT)
//    @PostMapping("/upload")
//    public AjaxResult uploadFile(
//        @RequestParam("file") MultipartFile file,
//        @RequestParam("courseCategory") String category,
//        @RequestParam("courseName") String name,
//        @RequestParam("type") String type) {
//
//        try {
//            // 构建存储路径
//            String relativePath = "course/course_doc/" + category + "/" + name + "/" + type;
//            Path targetPath = Paths.get("public/", relativePath);
//
//            // 创建目录并保存文件
//            Files.createDirectories(targetPath);
//            Files.copy(file.getInputStream(), targetPath.resolve(file.getOriginalFilename()));
//
//            // 生成访问URL
//            String accessUrl = "http://localhost:85/" + relativePath + "/" + file.getOriginalFilename();
//
//            logger.info("relativePath: {}", relativePath);
//            logger.info("targetPath: {}", targetPath);
//            logger.info("accessUrl: {}", accessUrl);
//            return AjaxResult.success().put("fileUrl", accessUrl);
//        } catch (IOException e) {
//            logger.error("文件上传失败", e);
//            return AjaxResult.error(e.getMessage());
//        }
//    }

    /**
     * 修改课程文件
     */
    @PreAuthorize("@ss.hasPermi('course:document:edit')")
    @Log(title = "课程文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UoocCourseDocument uoocCourseDocument)
    {
        return toAjax(uoocCourseDocumentService.updateUoocCourseDocument(uoocCourseDocument));
    }

    /**
     * 删除课程文件
     */
    @PreAuthorize("@ss.hasPermi('course:document:remove')")
    @Log(title = "课程文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(uoocCourseDocumentService.deleteUoocCourseDocumentByRecordIds(recordIds));
    }
}
